package task1;

public class CalculationApplication {

	final static int THREAD_COUNT = 4;
	final static double N = 1000.0;
	final static double STEP = 0.0001;
	volatile static double results = 0.0;
	
	public static synchronized void addToResult(double arg) {
		results += arg;
	}

	public static void main(String[] args) {
		CalculationThread[] threads = new CalculationThread[THREAD_COUNT];
		double partForThread = (2*N) / THREAD_COUNT;

		for (int i = 0; i < THREAD_COUNT; i++) {
			double myStart = partForThread * i - N;
			double myEnd = myStart + partForThread;
			if (i > 0) {
				myStart += STEP;
			}
			threads[i] = new CalculationThread(myStart, myEnd, STEP);
		}

		for (Thread thread : threads) {
			thread.start();
		}

		for (Thread thread : threads) {
			try {
				thread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		System.out.println("FINAL RESULT: " + results);
	}
}